﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace MadProps.Validation
{
    public class DataAnnotationsValidator : IValidator
    {
        public ValidationResult Validate(ValidationArgs args)
        {
            var displayName = Meta.DisplayName.Of(args.Prop);

            // TODO gotta do better on how we get these attributes...
            var messages = args.PropId.ClrMember.GetCustomAttributes(true)
                .OfType<ValidationAttribute>()
                .Where(att => !att.IsValid(args.Prop.Value))
                .Select(att => att.FormatErrorMessage(displayName))
                .ToList();

            if (messages.Count == 0)
            {
                return ValidationResult.Success();
            }
            else
            {
                return ValidationResult.Error(messages);
            }
        }
    }
}
